package com.heima.behavior.service.impl;

import com.heima.behavior.service.IApArticleBehaviorService;
import com.heima.behavior.service.IApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollectionBehavior;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
@Slf4j
public class ApArticleBehaviorService implements IApArticleBehaviorService {

    @Autowired
    private IApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * APP 端-文章详情页-加载登录用户对当前文章的所有行为
     * @param articleBehaviorDTO
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO articleBehaviorDTO) {
        //1.校验参数
            //1.1校验登录状态
        Integer loginId = AppThreadLocalUtils.getUser();
        //  boolean isfollow = false, islike = false, isunlike = false, iscollection = false;
        boolean islike = false; boolean isunlike = false; boolean iscollection = false; boolean isfollow = false;
        if(loginId != null && loginId.intValue() != 0){
            //1.2获取用户的行为实体
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(loginId, null);
            if(apBehaviorEntry == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "获取用户的行为实体失败");
            }
            //2.查询所有的用户行为
            Query query = Query.query(Criteria.where("entry_id").is(apBehaviorEntry.getId()).and("article_id").is(articleBehaviorDTO.getArticleId()));
                //2.1点赞行为
            ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if(apLikesBehavior != null){
                islike = true;
            }
                //2.2不喜欢行为
            ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if(apUnlikesBehavior != null){
                isunlike = true;
            }
                //2.3收藏行为
            ApCollectionBehavior apCollectionBehavior = mongoTemplate.findOne(query, ApCollectionBehavior.class);
            if(apCollectionBehavior != null){
                iscollection = true;
            }
                //2.4关注行为
            Double followScore = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + loginId, articleBehaviorDTO.getAuthorApUserId());
            if(followScore != null){
                isfollow = true;
            }
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("islike", islike);
        resultMap.put("isunlike", isunlike);
        resultMap.put("iscollection",iscollection);
        resultMap.put("isfollow", isfollow);
        //3.返回响应
        return ResponseResult.okResult(resultMap);
    }
}
